package com.controller;

import cn.hutool.core.io.FileUtil;
import com.github.pagehelper.PageInfo;
import com.pojo.Notice;
import com.pojo.Owners;
import com.service.NoticeService;
import com.service.OwnersService;
import com.vo.NoticeVo;
import com.vo.OwnersVo;
import com.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/owner")
public class OwnersController {
    @Resource
    private OwnersService ownersService;

    @Resource
    private NoticeService noticeService;

    @RequestMapping("/show")
    public String toowner(){
        return "ownerChange/owner";
    }

    @RequestMapping("/count")
    @ResponseBody
    public Result count(String phone){
        Owners owners = ownersService.selectidByphone(phone);
        if(owners!=null){
           return Result.success("查到了");
        }else{
            return Result.success("查无此人");
        }
    }
    @RequestMapping("/all")
    @ResponseBody
    public Result all(OwnersVo ownersVo){
        PageInfo<Owners> selectownerall = ownersService.selectownerall(ownersVo);
        return Result.success(selectownerall);
    }

    @RequestMapping("/notice")
    public String tonotice(){
        return "ownerChange/notice";
    }

    @RequestMapping("/notice/all")
    @ResponseBody
    public Result result(NoticeVo noticeVo){
        System.out.println(noticeVo);
        PageInfo<Notice> selectnoticeall = noticeService.selectnoticeall(noticeVo);
        return Result.success(selectnoticeall);
    }


    /**
     * 1.单文件上传
     * @param attach
     * @param request
     * @return
     */
    @RequestMapping("/uploads")
    @ResponseBody
    public Result uploads(@RequestParam("attach") MultipartFile attach, HttpServletRequest request){
        //1.获取上传的原始文件名
        String filename = attach.getOriginalFilename();
        //2.截取文件名的后缀
        String suffix = FileUtil.getSuffix(filename);
        //3.使用UUID生成新的文件名与后缀名进行拼接
        String newFileName = UUID.randomUUID().toString().replace("-","") + "." + suffix;
        //4.获取上传到服务器的文件夹的位置
        String folder = request.getServletContext().getRealPath("/attach");
        System.out.println(folder);
        try {
            //5.将上传的文件拷贝到attach中
            FileCopyUtils.copy(attach.getBytes(),new File(folder,newFileName));
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("attachPath","attach/"+newFileName);
            return Result.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("上传失败");
        }
    }
    //修改数据库
    @RequestMapping("/updatePath")
    @ResponseBody
    public Result updatePath( Notice notice){
        System.out.println(notice);
        try {
            int i = noticeService.updateByPrimaryKeySelective(notice);
            return Result.success("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("上传失败");
        }
    }
    //修改数据库
    @RequestMapping("/addnotice")
    @ResponseBody
    public Result addnotice( Notice notice){
        System.out.println(notice);
        try {
            LocalDateTime dateTime = LocalDateTime.now();
            notice.setCreatetime(dateTime);
            int i = noticeService.insertSelective(notice);
            return Result.success("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("上传失败");
        }
    }

    //修改数据库
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Integer id){
        try {
            int i = noticeService.deleteByPrimaryKey(id);
            return Result.success("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("删除失败");
        }
    }
    @RequestMapping("/selectOwnerByName")
    @ResponseBody
    public Result selectOwnerByName(String ownername){
        Owners owners = ownersService.selectOwnersByName(ownername);
        return Result.success(owners);
    }
    @RequestMapping("/selectOwnerById")
    @ResponseBody
    public Result selectOwnerById(Integer id){
        Owners owners = ownersService.selectOwnerById(id);
        return Result.success(owners);
    }

    @RequestMapping("/selectAllOwners")
    @ResponseBody
    public Result selectAllOwners(){
        List<Owners> owners = ownersService.selectAllOwner();
        return Result.success(owners);
    }
    @RequestMapping("/selectHaveCar")
    @ResponseBody
    public Result selectHaveCar(){
        List<Owners> owners = ownersService.selectOwnerHaveCar();
        return Result.success(owners);
    }

}
